package com.elective.courseselect.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elective.courseselect.entity.CourseEntity;
import com.elective.courseselect.entity.TeacherEntity;
import com.elective.courseselect.service.TeacherService;
import com.elective.courseselect.serviceUtils.UserUtils;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
 /**
 * 
 *
 * @author lee
 * @email 943204006@qq.ccom
 * @date 2021-09-04 19:22:34
 */
@RestController
@RequestMapping("courseselect/teacher")
@CrossOrigin(origins = "*",maxAge = 3600)
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = teacherService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        QueryWrapper<TeacherEntity> teacherEntityQueryWrapper = new QueryWrapper<>();
        teacherEntityQueryWrapper.eq("user_id", id);
        TeacherEntity one = teacherService.getOne(teacherEntityQueryWrapper);
        TeacherEntity teacher = teacherService.getById(one.getId());
        Boolean abandon = UserUtils.isAbandon(teacher.getUserId());
        if (abandon) {
            return R.error(StatusCode.ERROR,"老师账号已被弃用");
        }
        return R.ok().put("teacher", teacher);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody TeacherEntity teacher){
		teacherService.save(teacher);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody TeacherEntity teacher){
		teacherService.updateById(teacher);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		teacherService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


     @ApiOperation(value="查询老师信息" , notes = "")
     @GetMapping("/searchTeacherInfos/{userId}")
     public R searchTeacherInfos(@PathVariable Integer userId) {
         TeacherEntity teacherEntity = teacherService.searchTeacherInfos(userId);
         return R.ok(StatusCode.OK, "查询老师信息成功").put("teacherEntity", teacherEntity);
     }


     @ApiOperation(value="查询老师信息2" , notes = "")
     @GetMapping("/searchTeacherInfos2/{userId}")
     public R searchTeacherInfos2(@PathVariable Integer userId) {
         TeacherEntity teacherEntity = teacherService.searchTeacherInfos(userId);
         JSONObject jsonObjectNo1 = new JSONObject();
         jsonObjectNo1.put("data", teacherEntity);
         return R.ok(StatusCode.OK, "查询老师信息成功").put("teacherEntity", jsonObjectNo1);
     }

     /**
      * 修改
      */
     @ApiOperation("修改教师个人信息")
     @PostMapping("/updateTeacherInfo")
     public R updateTeacherInfo(@RequestBody TeacherEntity teacher){
         teacherService.updateTeacherInfo(teacher);
         return R.ok();
     }

     @ApiOperation(value="查看课程列表" , notes = "")
     @GetMapping("/searchTeacherCourse/{userId}")
     public R searchTeacherCourse(@PathVariable Integer userId) {
         List<CourseEntity> teacherCourseVo =  teacherService.searchTeacherCourse(userId);
         return R.ok(StatusCode.OK, "查询老师查看课程列表成功").put("teacherEntity", teacherCourseVo);
     }






     @ApiOperation(value="老师判题" , notes = "")
     @PostMapping("/teacherJudgeObj")
     public R teacherJudgeObj(@RequestBody Map<String,Object> map) {
         teacherService.teacherJudgeObj(map);
         return R.ok(StatusCode.OK, "老师主观题判题成功");
     }

}
